package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author itlxc
 * @since 2024-08-06
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class NursingProjectServiceImpl implements NursingProjectService {

    private final NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询护理项目
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.getByPage(name, status);
        return PageResponse.of(page, NursingProjectVo.class);
    }

    /**
     * 添加护理项目
     * @param nursingProjectDTO
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDTO, NursingProject.class);
        int count = nursingProjectMapper.save(nursingProject);
        if (count != 1) {
            throw new BaseException("添加护理项目失败");
        }
    }

    /**
     * 更新护理项目信息
     * @param nursingProjectDTO
     */
    @Override
    public void updateProject(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDTO, NursingProject.class);
        int count = nursingProjectMapper.updateProject(nursingProject);
        if (count != 1) {
            throw new BaseException("更新护理项目失败");
        }
    }

    /**
     * 启用或禁用护理项目
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        int count = nursingProjectMapper.updateProject(nursingProject);
        if (count != 1) {
            throw new BaseException("启用或禁用护理项目失败");
        }
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void deleteById(Long id) {
       nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProject> all() {
        return nursingProjectMapper.list();
    }

    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.getById(id);
    }
}
